<template>
	<view class="container">
		<view class="header">
			<text class="title">彩票中奖金额计算器</text>
		</view>

		<view class="game-type-selector">
			<view class="game-type-item" :class="{ 'active': gameType === item.id }" v-for="item in gameTypes"
				:key="item.id" @click="gameType = item.id">
				{{ item.name }}
			</view>
		</view>

		<view class="content">
			<view class="section">
				<view class="section-title">今日中奖号码</view>
				<view class="ball-container">
					<view class="ball-group">
						<view class="group-title">{{ currentConfig.regularName }}</view>
						<view class="balls">
							<view class="ball" :class="{ 'selected': todayRegularBalls.includes((ball + 1)) }"
								v-for="ball in currentConfig.regularRange" :key="ball"
								@click="toggleTodayRegularBall((ball + 1))">
								{{ (ball + 1) < 10 ? '0' + (ball + 1) : (ball + 1) }}
							</view>
						</view>
						<view class="selection-info">
							已选: {{ todayRegularBalls.length }}/{{ currentConfig.regularCount }}
						</view>
					</view>

					<view class="ball-group">
						<view class="group-title">{{ currentConfig.specialName }}</view>
						<view class="balls">
							<view class="ball special" :class="{ 'selected': todaySpecialBalls.includes((ball + 1)) }"
								v-for="ball in currentConfig.specialRange" :key="ball"
								@click="toggleTodaySpecialBall((ball + 1))">
								{{ (ball + 1) < 10 ? '0' + (ball + 1) : (ball + 1) }}
							</view>
						</view>
						<view class="selection-info">
							已选: {{ todaySpecialBalls.length }}/{{ currentConfig.specialCount }}
						</view>
					</view>
				</view>
			</view>

			<view class="section">
				<view class="section-title">我的号码</view>
				<view class="ball-container">
					<view class="ball-group">
						<view class="group-title">{{ currentConfig.regularName }}</view>
						<view class="balls">
							<view class="ball" :class="{ 'selected': userRegularBalls.includes((ball + 1)) }"
								v-for="ball in currentConfig.regularRange" :key="ball" @click="toggleRegularBall((ball + 1))">
								{{ (ball + 1) < 10 ? '0' + (ball + 1) : (ball + 1) }}
							</view>
						</view>
						<view class="selection-info">
							已选: {{ userRegularBalls.length }}/{{ currentConfig.regularCount }}
						</view>
					</view>

					<view class="ball-group">
						<view class="group-title">{{ currentConfig.specialName }}</view>
						<view class="balls">
							<view class="ball special" :class="{ 'selected': userSpecialBalls.includes((ball + 1)) }"
								v-for="ball in currentConfig.specialRange" :key="ball" @click="toggleSpecialBall((ball + 1))">
								{{ (ball + 1) < 10 ? '0' + (ball + 1) : (ball + 1) }}
							</view>
						</view>
						<view class="selection-info">
							已选: {{ userSpecialBalls.length }}/{{ currentConfig.specialCount }}
						</view>
					</view>
				</view>
			</view>

			<view class="btn-group">
				<button class="random-btn" @click="generateRandom">随机选号</button>
				<button class="calculate-btn" @click="calculatePrize">计算中奖金额</button>
			</view>

			<view class="result" v-if="resultAmount > 0">
				<text class="result-title">恭喜您!</text>
				<text class="prize-amount">您中了 {{ resultAmount }} 元</text>
			</view>
		</view>

		<view class="rules">
			<view class="rules-title">中奖规则:</view>
			<view class="rule-item" v-for="(rule, index) in currentRules" :key="index">
				<text>{{ getRuleDescription(rule) }}</text>
				<text>{{ rule.prize }}元</text>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				gameType: 'ssq',
				todayRegularBalls: [],
				todaySpecialBalls: [],
				userRegularBalls: [],
				userSpecialBalls: [],
				resultAmount: 0,
				gameTypes: [{
						id: 'ssq',
						name: '双色球'
					},
					{
						id: 'dlt',
						name: '大乐透'
					},
					{
						id: 'qlc',
						name: '七乐彩'
					}
				],
				gameConfig: {
					ssq: {
						regularCount: 6,
						regularRange: 33,
						specialCount: 1,
						specialRange: 16,
						regularName: '红球',
						specialName: '蓝球'
					},
					dlt: {
						regularCount: 5,
						regularRange: 35,
						specialCount: 2,
						specialRange: 12,
						regularName: '前区',
						specialName: '后区'
					},
					qlc: {
						regularCount: 7,
						regularRange: 30,
						specialCount: 1,
						specialRange: 30,
						regularName: '基本号码',
						specialName: '特别号码'
					}
				},
				prizeRules: {
					ssq: [{
							matchCount: 6,
							specialMatch: true,
							prize: 5000000
						},
						{
							matchCount: 6,
							specialMatch: false,
							prize: 200000
						},
						{
							matchCount: 5,
							specialMatch: true,
							prize: 3000
						},
						{
							matchCount: 5,
							specialMatch: false,
							prize: 200
						},
						{
							matchCount: 4,
							specialMatch: true,
							prize: 100
						},
						{
							matchCount: 4,
							specialMatch: false,
							prize: 10
						},
						{
							matchCount: 3,
							specialMatch: true,
							prize: 10
						},
						{
							matchCount: 2,
							specialMatch: true,
							prize: 5
						}
					],
					dlt: [{
							frontMatch: 5,
							backMatch: 2,
							prize: 10000000
						},
						{
							frontMatch: 5,
							backMatch: 1,
							prize: 500000
						},
						{
							frontMatch: 5,
							backMatch: 0,
							prize: 10000
						},
						{
							frontMatch: 4,
							backMatch: 2,
							prize: 3000
						},
						{
							frontMatch: 4,
							backMatch: 1,
							prize: 600
						},
						{
							frontMatch: 3,
							backMatch: 2,
							prize: 100
						},
						{
							frontMatch: 4,
							backMatch: 0,
							prize: 10
						},
						{
							frontMatch: 3,
							backMatch: 1,
							prize: 10
						},
						{
							frontMatch: 2,
							backMatch: 2,
							prize: 5
						},
						{
							frontMatch: 3,
							backMatch: 0,
							prize: 5
						},
						{
							frontMatch: 1,
							backMatch: 2,
							prize: 5
						},
						{
							frontMatch: 0,
							backMatch: 2,
							prize: 5
						}
					],
					qlc: [{
							matchCount: 7,
							prize: 5000000
						},
						{
							matchCount: 6,
							specialMatch: true,
							prize: 20000
						},
						{
							matchCount: 6,
							specialMatch: false,
							prize: 600
						},
						{
							matchCount: 5,
							specialMatch: true,
							prize: 50
						},
						{
							matchCount: 5,
							specialMatch: false,
							prize: 10
						},
						{
							matchCount: 4,
							specialMatch: true,
							prize: 10
						},
						{
							matchCount: 4,
							specialMatch: false,
							prize: 5
						},
						{
							matchCount: 3,
							specialMatch: true,
							prize: 5
						}
					]
				}
			}
		},
		computed: {
			currentConfig() {
				return this.gameConfig[this.gameType];
			},
			currentRules() {
				return this.prizeRules[this.gameType];
			},
			isRegularComplete() {
				return this.userRegularBalls.length === this.currentConfig.regularCount;
			},
			isSpecialComplete() {
				return this.userSpecialBalls.length === this.currentConfig.specialCount;
			},
			isTodayRegularComplete() {
				return this.todayRegularBalls.length === this.currentConfig.regularCount;
			},
			isTodaySpecialComplete() {
				return this.todaySpecialBalls.length === this.currentConfig.specialCount;
			}
		},
		watch: {
			gameType: {
				handler() {
					this.resetBalls();
				},
				immediate: true
			}
		},
		methods: {
			resetBalls() {
				this.todayRegularBalls = [];
				this.todaySpecialBalls = [];
				this.userRegularBalls = [];
				this.userSpecialBalls = [];
				this.resultAmount = 0;
			},

			toggleRegularBall(ball) {
				if (this.userRegularBalls.includes(ball)) {
					this.userRegularBalls = this.userRegularBalls.filter(b => b !== ball);
				} else if (this.userRegularBalls.length < this.currentConfig.regularCount) {
					this.userRegularBalls.push(ball);
					this.userRegularBalls.sort((a, b) => a - b);
				}
			},

			toggleSpecialBall(ball) {
				if (this.userSpecialBalls.includes(ball)) {
					this.userSpecialBalls = this.userSpecialBalls.filter(b => b !== ball);
				} else if (this.userSpecialBalls.length < this.currentConfig.specialCount) {
					this.userSpecialBalls.push(ball);
					this.userSpecialBalls.sort((a, b) => a - b);
				}
			},

			toggleTodayRegularBall(ball) {
				if (this.todayRegularBalls.includes(ball)) {
					this.todayRegularBalls = this.todayRegularBalls.filter(b => b !== ball);
				} else if (this.todayRegularBalls.length < this.currentConfig.regularCount) {
					this.todayRegularBalls.push(ball);
					this.todayRegularBalls.sort((a, b) => a - b);
				}
			},

			toggleTodaySpecialBall(ball) {
				if (this.todaySpecialBalls.includes(ball)) {
					this.todaySpecialBalls = this.todaySpecialBalls.filter(b => b !== ball);
				} else if (this.todaySpecialBalls.length < this.currentConfig.specialCount) {
					this.todaySpecialBalls.push(ball);
					this.todaySpecialBalls.sort((a, b) => a - b);
				}
			},

			calculatePrize() {
				if (!this.isTodayRegularComplete || !this.isTodaySpecialComplete ||
					!this.isRegularComplete || !this.isSpecialComplete) {
					uni.showToast({
						title: '请选择完整的号码',
						icon: 'none'
					});
					return;
				}

				try {
					let prize = 0;

					if (this.gameType === 'ssq') {
						prize = this.calculateSSQ();
					} else if (this.gameType === 'dlt') {
						prize = this.calculateDLT();
					} else if (this.gameType === 'qlc') {
						prize = this.calculateQLC();
					}

					this.resultAmount = prize;
					uni.showModal({
						title: '中奖结果',
						content: `恭喜您中了 ${prize} 元！`,
						showCancel: false
					});
				} catch (error) {
					uni.showToast({
						title: '计算失败，请重试',
						icon: 'none'
					});
					console.error(error);
				}
			},

			calculateSSQ() {
				let regularMatchCount = 0;
				let specialMatch = this.userSpecialBalls[0] === this.todaySpecialBalls[0];

				for (let num of this.userRegularBalls) {
					if (this.todayRegularBalls.includes(num)) {
						regularMatchCount++;
					}
				}

				for (let rule of this.currentRules) {
					if (regularMatchCount === rule.matchCount &&
						specialMatch === rule.specialMatch) {
						return rule.prize;
					}
				}

				return 0;
			},

			calculateDLT() {
				let frontMatch = 0;
				let backMatch = 0;

				for (let num of this.userRegularBalls) {
					if (this.todayRegularBalls.includes(num)) {
						frontMatch++;
					}
				}

				for (let num of this.userSpecialBalls) {
					if (this.todaySpecialBalls.includes(num)) {
						backMatch++;
					}
				}

				for (let rule of this.currentRules) {
					if (frontMatch === rule.frontMatch && backMatch === rule.backMatch) {
						return rule.prize;
					}
				}

				return 0;
			},

			calculateQLC() {
				let regularMatchCount = 0;
				let specialMatch = this.userSpecialBalls[0] === this.todaySpecialBalls[0];

				for (let num of this.userRegularBalls) {
					if (this.todayRegularBalls.includes(num)) {
						regularMatchCount++;
					}
				}

				for (let rule of this.currentRules) {
					if (rule.specialMatch !== undefined) {
						if (regularMatchCount === rule.matchCount &&
							specialMatch === rule.specialMatch) {
							return rule.prize;
						}
					} else {
						if (regularMatchCount === rule.matchCount) {
							return rule.prize;
						}
					}
				}

				return 0;
			},

			getRuleDescription(rule) {
				if (this.gameType === 'dlt') {
					return `${rule.frontMatch}个前区 + ${rule.backMatch}个后区`;
				} else if (rule.specialMatch !== undefined) {
					return `${rule.matchCount}个${this.currentConfig.regularName} + ${rule.specialMatch ? '特别号码' : '无特别号码'}`;
				} else {
					return `${rule.matchCount}个${this.currentConfig.regularName}`;
				}
			},

			generateRandom() {
				this.resetBalls();

				// 随机生成开奖号码
				this.todayRegularBalls = this.generateRandomNumbers(
					this.currentConfig.regularCount,
					1,
					this.currentConfig.regularRange
				);

				this.todaySpecialBalls = this.generateRandomNumbers(
					this.currentConfig.specialCount,
					1,
					this.currentConfig.specialRange
				);

				// 随机生成用户号码
				this.userRegularBalls = this.generateRandomNumbers(
					this.currentConfig.regularCount,
					1,
					this.currentConfig.regularRange
				);

				this.userSpecialBalls = this.generateRandomNumbers(
					this.currentConfig.specialCount,
					1,
					this.currentConfig.specialRange
				);
			},

			generateRandomNumbers(count, min, max) {
				const numbers = new Set();
				while (numbers.size < count) {
					const num = Math.floor(Math.random() * (max - min + 1)) + min;
					numbers.add(num);
				}
				return Array.from(numbers).sort((a, b) => a - b);
			}
		}
	}
</script>
<style>
	.container {
		padding: 20rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	.header {
		text-align: center;
		padding: 30rpx 0;
	}

	.title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}

	.game-type-selector {
		display: flex;
		justify-content: center;
		margin-bottom: 30rpx;
	}

	.game-type-item {
		padding: 15rpx 30rpx;
		border: 1rpx solid #007aff;
		color: #007aff;
		margin: 0 10rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
	}

	.game-type-item.active {
		background-color: #007aff;
		color: #fff;
	}

	.content {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
	}

	.section {
		margin-bottom: 40rpx;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
	}

	.ball-container {
		display: flex;
		flex-direction: column;
	}

	.ball-group {
		margin-bottom: 30rpx;
	}

	.group-title {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 15rpx;
	}

	.balls {
		display: flex;
		flex-wrap: wrap;
	}

	.ball {
		width: 70rpx;
		height: 70rpx;
		border-radius: 50%;
		background-color: #f0f0f0;
		color: #333;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 10rpx;
		font-size: 28rpx;
		font-weight: bold;
	}

	.ball.special {
		background-color: #ffe6e6;
	}

	.ball.selected {
		background-color: #ff4500;
		color: #fff;
	}

	.ball.special.selected {
		background-color: #007aff;
	}

	.selection-info {
		font-size: 24rpx;
		color: #999;
		margin-top: 15rpx;
	}

	.btn-group {
		display: flex;
		justify-content: space-between;
		margin-top: 20rpx;
	}

	.random-btn,
	.calculate-btn {
		width: 48%;
		height: 90rpx;
		color: #fff;
		font-size: 32rpx;
		border-radius: 8rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.random-btn {
		background-color: #ff9500;
	}

	.calculate-btn {
		background-color: #007aff;
	}

	.result {
		margin-top: 40rpx;
		text-align: center;
	}

	.result-title {
		font-size: 32rpx;
		color: #ff4500;
		font-weight: bold;
	}

	.prize-amount {
		font-size: 48rpx;
		color: #ff4500;
		font-weight: bold;
		margin-top: 10rpx;
	}

	.rules {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 30rpx;
	}

	.rules-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
	}

	.rule-item {
		display: flex;
		justify-content: space-between;
		padding: 15rpx 0;
		border-bottom: 1rpx solid #eee;
		font-size: 28rpx;
	}

	.rule-item:last-child {
		border-bottom: none;
	}
</style>